//
//  ViewController.m
//  SanYiSanDemo
//
//  Created by GZMLUser on 14-10-27.
//  Copyright (c) 2014年 GZMLUser. All rights reserved.
//

#import "ViewController.h"
#import "IOS7SearchController.h"
#import <ZXingWidgetController.h>
#import <QRCodeReader.h>

#define IOS7 [[[UIDevice currentDevice] systemVersion]floatValue]>=7

@interface ViewController ()<ZXingDelegate,IOS7SearchDelegete,UIAlertViewDelegate>
{
    NSString *_searchResult;
    
    IOS7SearchController *ios7SearchController;
}
@end

@implementation ViewController

- (void)viewDidLoad {
    
    [super viewDidLoad];
    
    [self addTwoButton]; // 添加两个控制按钮
}

#pragma mark -添加两个版本控制按钮
- (void)addTwoButton
{
    // io6
    UIButton *beforIOS7Btn = [UIButton buttonWithType:UIButtonTypeCustom];
    [beforIOS7Btn setTitle:@"IOS6" forState:UIControlStateNormal];
    beforIOS7Btn.frame = CGRectMake(0, 200, 320, 50);
    [beforIOS7Btn addTarget:self action:@selector(gotoIOS6search) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:beforIOS7Btn];
    
    // io7
    UIButton *afterIOS7Btn = [UIButton buttonWithType:UIButtonTypeCustom];
    [afterIOS7Btn setTitle:@"IOS7" forState:UIControlStateNormal];
    afterIOS7Btn.frame = CGRectMake(0, 300, 320, 50);
    [afterIOS7Btn addTarget:self action:@selector(gotoIOS7search) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:afterIOS7Btn];
}

#pragma mark 按钮监听
- (void)gotoIOS6search
{
    [self startReadingBeforeIOS7];
}

- (void)gotoIOS7search
{
    [self startReadingafterIOS7];
}

#pragma mark - 扫描iOS7前版本
- (void)startReadingBeforeIOS7
{
    ZXingWidgetController *widController = [[ZXingWidgetController alloc]initWithDelegate:self showCancel:NO OneDMode:NO showLicense:NO];
    
    NSMutableSet *readers = [[NSMutableSet alloc]init];
    QRCodeReader *qrcodeReader = [[QRCodeReader alloc]init];
    [readers addObject:qrcodeReader];
    widController.readers = readers;
    [self.navigationController pushViewController:widController animated:YES];
}

#pragma mark -扫描IOS7后版本
- (void)startReadingafterIOS7
{
    IOS7SearchController *ios7SearchViewController = [[IOS7SearchController alloc]initIOS7SearchWithDelegate:self];
    
    [self.navigationController pushViewController:ios7SearchViewController animated:YES];
}


#pragma mark -提示框
- (void)showSearchMessageWithTag:(NSInteger)versionTag;
{
    UIAlertView *av = [[UIAlertView alloc]initWithTitle:@"扫描结果" message:_searchResult delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"确定", nil];
    av.tag = versionTag;
    [av show];
}

#pragma mark -ZXing 代理方法
- (void)zxingController:(ZXingWidgetController*)controller didScanResult:(NSString *)result
{
    NSLog(@"响应ZXing获取结果方法");
}
-(void)zxingControllerDidCancel:(ZXingWidgetController *)controller
{
    // 初始化时打开cancel按钮才会调用此方法
    
}
#pragma mark -ios7扫描代理方法
- (void)ios7SearchController:(IOS7SearchController *)controller didScanResult:(NSString *)result
{
    NSLog(@"响应IOS7Search代理方法  获取结果%@",result);
    ios7SearchController = controller;
    if (result) {
        _searchResult = result;
    }
    

}

// 当获取到结果，扫描结束 响应
- (void)ios7SearchControllerDidCancel:(IOS7SearchController *)controller
{
    [self showSearchMessageWithTag:101];
}

#pragma mark - alertView代理方法
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    if (buttonIndex == 0) {// 取消
        // 重新扫描
        if (alertView.tag == 100) { // ios6
            
        }
        if (alertView.tag == 101) {// ios7
            
            [ios7SearchController startIOS72DSearch];
        }
        
    } else if (buttonIndex == 1){
        // 打开链接
        if (alertView.tag == 100) { // ios6
            
        }
        if (alertView.tag == 101) {// ios7
            
            [ios7SearchController stopIOS72DSearch];
            [self.navigationController popViewControllerAnimated:YES];
        }
    }
}

#pragma mark -打开获取信息链接
- (void)openGetUrlMesage
{
    
}
@end
